package com.leetcode;

/**
 * 209. 长度最小的子数组
 * 滑动窗口  另一种编码
 */
public class Solution209_4 {

    public int minSubArrayLen(int target, int[] nums) {
        int n = nums.length;
        int l = 0;
        int r = -1;
        int res = n + 1;
        int sum = 0;
        while (l < n) {
            if (r + 1 < n && sum < target) {
                sum += nums[++r];
            } else {
                sum -= nums[l++];
            }
            if (sum >= target) {
                res = Math.min(res, r - l + 1);
            }
        }
        return res == n + 1 ? 0 : res;
    }

    public static void main(String[] args) {
        int nums[] = new int[]{2, 3, 1, 2, 4, 3};
        int target = 7;
        int minLength = new Solution209_4().minSubArrayLen(target, nums);
        System.out.println(minLength);
    }

}
